package com.hongyan.widget;

import com.hongyan.config.MainFrameConfig;
import com.hongyan.main.SwingMainFrame;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

public class ButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {

    protected JButton button;

    private JTable table;
    private String lable;
    private TableButtonClick tableButtonClick;

    public ButtonEditor(String lable, JTable table, TableButtonClick tableButtonClick) {
        button = new JButton();
        this.table = table;
        this.lable = lable;
        this.tableButtonClick = tableButtonClick;
        button.setActionCommand(lable);
        button.addActionListener(this);
        button.setBorderPainted(false);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        if (isSelected) {
            button.setForeground(table.getSelectionForeground());
            button.setBackground(table.getSelectionBackground());
        } else {
            button.setForeground(table.getForeground());
            button.setBackground(UIManager.getColor("Button.background"));
        }
        button.setText((lable == null) ? "" : lable);
        return button;
    }

    @Override
    public Object getCellEditorValue() {
        return lable;
    }

    @Override
    public boolean isCellEditable(EventObject e) {

        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        fireEditingStopped();
        String id = (String) table.getValueAt(table.getSelectedRow(), 0);

        SwingMainFrame swingMainFrame = MainFrameConfig.getSwingMainFrame();
        JDialog jDialog = new JDialog(swingMainFrame, String.format("请选择对ID为%s的数据的操作方式", id), true);
        jDialog.setSize(200, 80);

        JPanel jPanel = new JPanel();
        JPanel jPanel2 = new JPanel();
        jPanel.setLayout(new FlowLayout());
        jPanel2.setLayout(new BorderLayout());
        JButton edit = new JButton("修改");
        edit.setSize(120, 100);

        JButton delete = new JButton("删除");
        delete.setSize(120, 100);
        jPanel.add(edit);
        jPanel.add(delete);
        jPanel2.add(jPanel, BorderLayout.CENTER);
        jDialog.add(jPanel2);
        edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jDialog.dispose();
                tableButtonClick.onClick(1, id, null);
            }
        });
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jDialog.dispose();
                tableButtonClick.onClick(2, id, null);
            }
        });
        Dimension dialogSize = jDialog.getSize();
        Point frameLocation = swingMainFrame.getLocation();
        Dimension frameSize = swingMainFrame.getSize();

        int x = frameLocation.x + (frameSize.width - dialogSize.width) / 2;
        int y = frameLocation.y + (frameSize.height - dialogSize.height) / 2;

        // 设置对话框的位置
        jDialog.setLocation(x, y);

        jDialog.setVisible(true);

    }
}
